package com.codeoss.framework.webide.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.framework.webide.request.ReportWizardDialogRequest;
import com.codeoss.framework.webide.response.ReportWizardDialogResponse;
import com.codeoss.framework.webide.service.IReportWizardDialogService;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.IIrDictService;
import com.codeoss.module.ir.service.IIrReportService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class ReportWizardDialogService implements IReportWizardDialogService {
	@Autowired
	private IIrReportService irReportService;
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
    private IIrDictService irDictService;
	@Override
	@Transactional
	public ReportWizardDialogResponse onFinish(ReportWizardDialogRequest request) {
		// TODO Auto-generated method stub
		ReportWizardDialogResponse reportWizardDialogResponse=new ReportWizardDialogResponse();
		ObjectNode objectNode=null;
		try {
			objectNode=(ObjectNode)objectMapper.readTree(request.getJson());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		String moduleType=objectNode.get("moduleTypeSelectPage").get("moduleType").asText();
		String code=objectNode.get("reportBaseInfoPage").get("code").asText();
		
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", code);
		IrReport irReport=irReportService.getOne(query);
		if(irReport!=null) {
			throw new RuntimeException("此编码已经存在");
		}
		
		String name=objectNode.get("reportBaseInfoPage").get("name").asText();
		String description=objectNode.get("reportBaseInfoPage").get("description").asText();
		String reportType=objectNode.get("reportTypeSelectPage").get("reportType").asText();
		
		irReport=new IrReport();
		irReport.setModuleType(moduleType);
		irReport.setCode(code);
		irReport.setName(name);
		irReport.setDescription(description);
		irReport.setReportType(reportType);
		irReportService.insert(irReport);
		
		Map<String,Object> data=new HashMap<String,Object>();
		reportWizardDialogResponse.setData(data);
		data.put("name", name);
		data.put("code", code);
		data.put("reportType", reportType);
		return reportWizardDialogResponse;
	}
	@Override
	public ReportWizardDialogResponse initModuleTypeSelectPage() {
		// TODO Auto-generated method stub
		ReportWizardDialogResponse reportWizardDialogResponse=new ReportWizardDialogResponse();
		List<ValueNamePair> moduleTypeList=irDictService.getPairListByCode("moduleType");
		reportWizardDialogResponse.setModuleTypeList(moduleTypeList);
		return reportWizardDialogResponse;
	}

}
